<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <script src="../js/babel.js"></script>
</head>

<body>
  <div id="root"></div>
</body>

</html>

<script type="text/babel">
  class A extends React.Component {
    state = {
      car: '奥拓'
    }

    convertCar = () => {
      this.setState({
        car: '奥迪'
      })
    }

    // 组件将要挂载
    componentWillMount() {
      console.log('A----componentWillMount');
    }

    // 组件挂载器之后
    componentDidMount() {
      console.log('A ----componentDidMount');
    }

    render() {
      console.log('A ----render');
      return (
        <div>
          <h2>组件A</h2>
          <B carName={this.state.car} />
          <button onClick={this.convertCar}>click me 换车</button>
        </div>
      )
    }
  }

  class B extends React.Component {
    constructor(props) {
      super(props)
      this.state = {}
      console.log('B----constructor');
    }

    componentWillMount() {
      console.log('B -----componentWillMount');
    }

    componentDidMount() {
      console.log('B -----componentDidMount');
    }

    // 组件将要接收参数 注意->第一次传递的参数不算数 也就是说 第一次传递参数不会触发这个钩子
    componentWillReceiveProps(props) {
      console.log('B ------componentWillReceiveProps', props);
    }

    shouldComponentUpdate() {
      console.log('B------shouldComponentUpdate');
      return true
    }

    // 组件将要更新
    componentWillUpdate() {
      console.log('B------componentWillUpdate');
    }

    // 组件更新之后
    componentDidUpdate() {
      console.log('B -----componentDidUpdate');
    }

    render() {
      console.log('B ----render');
      return (
        <div>
          <h2>组件B, 接收到的车是：{this.props.carName}</h2>
        </div>
      )
    }
  }

  ReactDOM.render(<A />, document.getElementById('root'))

</script>